package com.woorigil.social.parser.groupon;

import java.io.IOException;
import java.io.Reader;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.ParsingException;
import nu.xom.ValidityException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.woorigil.social.DateUtils;
import com.woorigil.social.domain.ProviderCode;
import com.woorigil.social.domain.deal.Deal;
import com.woorigil.social.domain.deal.DealListDocument;
import com.woorigil.social.domain.deal.ManageStatus;
import com.woorigil.social.parser.XmlParser;

@Component (value="groupXmlParser")
public class GrouponXmlParser extends XmlParser {
	private static Logger log = LoggerFactory.getLogger(GrouponXmlParser.class);
	
	@Value("${groupon.deal.path}") 
	String dealPath;
	
	@Override
	public String getCharsetName() {
		return "UTF-8";
	}

	@Override
	public ProviderCode getProviderCode() {
		return ProviderCode.GP;
	}
	
	@Override
	public String getSourcePath() {
		return dealPath;
	}
	
	@Override
	public DealListDocument parse(Reader reader) throws ValidityException, ParsingException, IOException {
		Builder builder = new Builder();
		Document doc = builder.build(reader);
		Element root = doc.getRootElement();
		
		DealListDocument dealListDocument = new DealListDocument();
		dealListDocument.setSourceProvider(getValue(root.getFirstChildElement("name")));
		dealListDocument.setVersion(getValue(root.getFirstChildElement("doc_ver")));
		dealListDocument.setHomepage(getValue(root.getFirstChildElement("url")));
		dealListDocument.setLogo(getValue(root.getFirstChildElement("logo_image")));
		
		Element dealsEle = root.getFirstChildElement("deals");
		Elements dealsEles = dealsEle.getChildElements();
		for (int i = 0; i < dealsEles.size(); i++) {
			Element dealEl = dealsEles.get(i);

			Deal deal = new Deal();
			deal.setProviderCode(getProviderCode());
			deal.setMetaId(getValue(dealEl.getFirstChildElement("meta_id")));
			deal.setTitle(getValue(dealEl.getFirstChildElement("title")));
			deal.setCategory(getValue(dealEl.getFirstChildElement("category")));
			deal.setSaleStartAt(DateUtils.toDate(getValue(dealEl.getFirstChildElement("start_at")), "yyyy-MM-dd HH:mm:ss"));
			deal.setSaleEndAt(DateUtils.toDate(getValue(dealEl.getFirstChildElement("end_at")), "yyyy-MM-dd HH:mm:ss"));
			deal.setUseStartAt(getValue(dealEl.getFirstChildElement("coupon_start_at")));
			deal.setUseEndAt(getValue(dealEl.getFirstChildElement("coupon_end_at")));
			deal.setWebLink(getValue(dealEl.getFirstChildElement("url")));
			deal.setMobileLink(getValue(dealEl.getFirstChildElement("mobile_url")));
			deal.setOriginalPrice(Integer.parseInt(getValue(dealEl.getFirstChildElement("original"))));
			deal.setSalesPrice(Integer.parseInt(getValue(dealEl.getFirstChildElement("price"))));
			deal.setDiscountRate(Integer.parseInt(getValue(dealEl.getFirstChildElement("discount"))));
			deal.setMaxCount(Integer.parseInt(getValue(dealEl.getFirstChildElement("max_count"))));
			deal.setMinCount(Integer.parseInt(getValue(dealEl.getFirstChildElement("min_count"))));
			deal.setNowCount(Integer.parseInt(getValue(dealEl.getFirstChildElement("now_count"))));
			deal.setSalesStatus(getValue(dealEl.getFirstChildElement("status")));
			
			Element imageEl = dealEl.getFirstChildElement("images");
			deal.setImageUrl1(getValue(imageEl.getFirstChildElement("image")));
			
			Element shopsEl = dealEl.getFirstChildElement("shops");
			Elements shopsEls = shopsEl.getChildElements();
			for (int k = 0; k < shopsEls.size(); k++) {
				if (k == 0) {
					deal.setShopName(getValue(dealEl.getFirstChildElement("shop_name")));
					deal.setShopAddress(getValue(dealEl.getFirstChildElement("shop_address")));
					deal.setShopTelephone(getValue(dealEl.getFirstChildElement("shop_tel")));
					deal.setShopLatitude(getValue(dealEl.getFirstChildElement("lat")));
					deal.setShopLongitude(getValue(dealEl.getFirstChildElement("lng")));
				}
			}
			deal.setImageUrl(deal.getImageUrl1());
			deal.setAverageCount((int)(deal.getNowCount() / DateUtils.getTodayCount(deal.getSaleStartAt())));
			deal.setManageStatus(ManageStatus.S);
			deal.setCategoryId(findCategoryId(getProviderCode(), deal));
			dealListDocument.add(deal);
		}
		
		log.info(getProviderCode() + " deal size : " + dealListDocument.getDeals().size());
		
		return dealListDocument;
	}
}
